package com.haozhen.sortnumber;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Arrays;

/**
 * 驱动类
 */
public class SortNumberDriver {

    private static  Logger logger = Logger.getLogger(SortNumberDriver.class);

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 校验参数
        if(args.length!=2) {
            logger.info("参数个数有误");
            System.exit(0);
        }
        // 输入文件是第一个参数 且用逗号隔离
        Path[] inputPath = Arrays.asList(args[0].split(",")).stream().map(Path::new).toArray(Path[]::new);
        //初始化配置对象
        Configuration conf = new Configuration();
        //初始化任务
        Job job = Job.getInstance(conf, "SortNumberDriver");
        //配置信息
        job.setJarByClass(SortNumberDriver.class);
        job.setMapperClass(SortNumberMapper.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setReducerClass(SortNumberReducer.class);

        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,inputPath);

        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        job.waitForCompletion(true);
    }
}
